//
//  ViewController.m
//  GCD其它的一些常见函数的使用
//
//  Created by hyl on 16/7/18.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (strong,nonatomic) UIImage *image1;
@property (strong,nonatomic) UIImage *image2;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [self barrier];
//    [self delay];
//    [self once];
//    [self moveFile];
//    [self moveFileWithApply];
//    [self group];
    [self imageWithGroup];
}

#pragma mark - imageWithGroup
//目标：下载两张图片，再将两张图片拼接起来
-(void) imageWithGroup{
    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group=dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        NSString *strUrl=@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg";
        NSURL *url=[NSURL URLWithString:strUrl];
        NSData *data=[NSData dataWithContentsOfURL:url];
        self.image1=[UIImage imageWithData:data];
    });
    dispatch_group_async(group, queue, ^{
        NSString *strUrl=@"http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg";
        NSURL *url=[NSURL URLWithString:strUrl];
        NSData *data=[NSData dataWithContentsOfURL:url];
        self.image2=[UIImage imageWithData:data];
    });
    dispatch_group_notify(group, queue, ^{
        UIGraphicsBeginImageContext(CGSizeMake(100, 100));
        [self.image1 drawInRect:CGRectMake(0, 0, 50, 100)];
        [self.image2 drawInRect:CGRectMake(50, 0, 50, 100)];
        UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
        dispatch_async(dispatch_get_main_queue(), ^{
            self.imageView.image=image;
        });
        UIGraphicsEndImageContext();
    });
}
#pragma mark - group
//dispatch_group_notify 必定是前面的都执行完成后才执行
-(void) group{
    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group=dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        NSLog(@"start");
    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"11");
    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"22");
    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"33");
    });
    dispatch_group_notify(group, queue, ^{
        NSLog(@"end");
    });
}

#pragma mark - moveFile
//这种方式开启多线程来处理
-(void) moveFile{
    NSString *from=@"/Users/hyl/Desktop/IOS/pictures";
    NSString *to=@"/Users/hyl/Desktop/团购";
    NSFileManager *mgr=[NSFileManager defaultManager];
    NSArray *subPaths=[mgr subpathsAtPath:from];
    for (NSString *subPath in subPaths) {
        //        NSString *fromFullPath=[from stringByAppendingString:subPath];//会少一个/
        NSString *fromFullPath=[from stringByAppendingPathComponent:subPath];
        NSString *toFullPath=[to stringByAppendingPathComponent:subPath];
        //        NSLog(@"%@",fromFullPath);
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [mgr moveItemAtPath:fromFullPath toPath:toFullPath error:nil];
            NSLog(@"%@  %@",[NSThread currentThread],subPath);
        });
    }
}


#pragma mark - moveFileWithApply
//快速迭代
-(void) moveFileWithApply{
    NSString *from=@"/Users/hyl/Desktop/IOS/pictures";
    NSString *to=@"/Users/hyl/Desktop/团购";
    NSFileManager *mgr=[NSFileManager defaultManager];
    NSArray *subPaths=[mgr subpathsAtPath:from];
    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_apply(subPaths.count, queue, ^(size_t index) {
        NSString *subPath=subPaths[index];
        NSString *fromFullPath=[from stringByAppendingPathComponent:subPath];
        NSString *toFullPath=[to stringByAppendingPathComponent:subPath];
        [mgr moveItemAtPath:fromFullPath toPath:toFullPath error:nil];
        NSLog(@"%@  %@",[NSThread currentThread],subPath);
    });
}

#pragma mark - onece
//onceToken记录执行的次数
//只会执行一次不会再执行
-(void) once{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"dispatch_once");
    });
}

#pragma mark - delay
-(void) delay{
//    第一种：performSelector
//    [self performSelector:@selector(run) withObject:nil afterDelay:2.0];
//    第二种：dispatch_after
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        NSLog(@"2秒");
//    });
    
//    第三种：NSTimer
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];
}
-(void) run{
    NSLog(@"delay");
}

#pragma mark - barrier
//1.全局并行队列是不行的，必需自创队列，并且是异步才可以
//2.barrier会等barrier前面的异步函数执行完成后，再执行barrier，再执行后面的异步函数
-(void) barrier{
//    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_queue_t queue=dispatch_queue_create("barrier", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"%@ --1-barrier",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@ --2-barrier",[NSThread currentThread]);
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"dispatch_barrier_async");
    });
    dispatch_async(queue, ^{
        NSLog(@"%@ --3-barrier",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@ --3-barrier",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@ --3-barrier",[NSThread currentThread]);
    });
}
@end
